package eu.testing.system.controllers;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;

import eu.testing.system.dao.TestDao;
import eu.testing.system.models.Result;
import eu.testing.system.models.Test;

@Controller
public class TestController {
	SystemController controller = new SystemController();

	@RequestMapping(value = "/exploreTests", method = RequestMethod.GET)
	public String exploreTests(ModelMap model) {
		model = controller.addUserData(model);
		List<Test> tests;
		try {
			if (controller.isAdmin()) {
				tests = controller.testDao.getAllTests();

			} else {
				tests = controller.testDao.getSpecificTests(controller.getUsername());
			}
			System.out.println(tests.size());
			model.addAttribute("tests", tests);

		} catch (SQLException e) {
			e.printStackTrace();
		}

		return "exploreTests";

	}

	@RequestMapping(value = "/exploreSingleTest", method = RequestMethod.GET)
	public String exploreSingleTest(@RequestParam(value = "testId") int testId,
			ModelMap model) throws SQLException {
		model = controller.addUserData(model);
		Test requestedTest = controller.testDao.getTest(testId);
		model.addAttribute("requestedTest", requestedTest);
		return "exploreSingleTest";

	}

	@RequestMapping(value = "/createTest", method = RequestMethod.GET)
	public String constructor(ModelMap model) throws IOException {
		model = controller.addUserData(model);
		Test test = new Test();
		model.addAttribute("test", test);
		return "testConstructor";
	}

	// TODO refactor action recognition
	@RequestMapping(value = "/createTest", method = RequestMethod.POST)
	@ResponseStatus(value = HttpStatus.OK)
	public String createNewTest(@RequestBody() String json, ModelMap model) {
		model = controller.addUserData(model);
		Test test = (Test) controller.getObjectFromJson(json, Test.class);
		controller.logger.log(Level.INFO,
				"New test received: " + test.getTitle());
		try {
			controller.testDao.insert(test);
			model.addAttribute("status", "Test '" + test.getTitle()
					+ "' successfully added!");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return "testCreationSubmit";

	}

	@RequestMapping(value = "/editTest", method = RequestMethod.POST)
	@ResponseStatus(value = HttpStatus.OK)
	public String editTest(@RequestBody() String json, ModelMap model) {
		model = controller.addUserData(model);
		Test test = (Test) controller.getObjectFromJson(json, Test.class);
		controller.logger.log(Level.INFO,
				"New test received for edition: " + test.getId());
		controller.testDao.editTest(test);
		model.addAttribute("status", "Test '" + test.getId()
				+ "' successfully edited!");
		return "testCreationSubmit";

	}

	@RequestMapping(value = "/testAction", method = RequestMethod.GET)
	public String testAction(@RequestParam(value = "action") String activity,
			ModelMap model) throws IOException, SQLException {
		String returnedAction = "exploreTests";
		model = controller.addUserData(model);

		String[] parts = activity.split("_");
		String action = parts[0];
		int id = Integer.parseInt(parts[1]);

		ApplicationContext context = new ClassPathXmlApplicationContext(
				"Spring-Module.xml");
		TestDao testDao = (TestDao) context.getBean("TestDao");
		if (action.equals("deleteTest")) {

			testDao.deleteTest(id);
			List<Test> tests = testDao.getAllTests();
			model.addAttribute("tests", tests);

		} else if (action.equals("editTest")) {
			Test testToEdit = testDao.getTest(id);
			model.addAttribute("requestedTest", testToEdit);
			returnedAction = "editSingleTest";
		}
		return returnedAction;

	}

	@RequestMapping(value = "/submitTest", method = RequestMethod.POST)
	@ResponseStatus(value = HttpStatus.OK)
	public String submitTest(@RequestBody() String json, ModelMap model)
			throws SQLException {
		model = controller.addUserData(model);
		Test submitedTest = (Test) controller.getObjectFromJson(json,
				Test.class);

		controller.logger.log(Level.INFO, "New test submited: "
				+ submitedTest.getTitle());
		System.out.println();
		Result result = controller.resultDao.analyzeAnswers(
				controller.getUsername(), submitedTest);
		controller.resultDao.insertResult(result);
		model.addAttribute("status",
				"You have submited test for "
						+ result.getScore() + "%");

		return "testCreationSubmit";

	}

}